package org.waddy.framework.pub.util.common;

import java.io.UnsupportedEncodingException;
import java.lang.Character.UnicodeBlock;

/**
 * @Title: 编码、解码、转码工具类
 * @version 1.0
 * @author zdp
 * @Created 2012-9-14
 * Copyright (c) 2012 PanGene InfoAnalytics.
 *
 * @Description: 编码、解码、转码工具类
 */
public class CodeUtil {
	
	/**
	 * 将GBK编码的字符串转换为UTF8编码的字符串 
	 * @param gbkStr 原始用GBK编码的字符串
	 * @return String 转换后用UTF-8编码的字符串
	 */
	public static String gbk2utf(String gbkStr) {
		String utf8str = null;
		char[] chars = gbkStr.toCharArray();
		byte[] fullByte = new byte[3 * chars.length];
		for (int i = 0; i < chars.length; i++) {
			int m = (int) chars[i];
			String word = Integer.toBinaryString(m);
			// System.out.println(word);

			StringBuffer sb = new StringBuffer();
			int len = 16 - word.length();
			// 补零
			for (int j = 0; j < len; j++) {
				sb.append("0");
			}
			sb.append(word);
			sb.insert(0, "1110");
			sb.insert(8, "10");
			sb.insert(16, "10");

			// System.out.println(sb.toString());

			String s1 = sb.substring(0, 8);
			String s2 = sb.substring(8, 16);
			String s3 = sb.substring(16);

			byte b0 = Integer.valueOf(s1, 2).byteValue();
			byte b1 = Integer.valueOf(s2, 2).byteValue();
			byte b2 = Integer.valueOf(s3, 2).byteValue();
			byte[] bf = new byte[3];
			bf[0] = b0;
			fullByte[i * 3] = bf[0];
			bf[1] = b1;
			fullByte[i * 3 + 1] = bf[1];
			bf[2] = b2;
			fullByte[i * 3 + 2] = bf[2];

		}
		try {
			utf8str = new String(fullByte,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		return utf8str;
	}
	
    /**
    * Unicode转UTF8
    * @param theString 原字符串
    * @return String 转码后字符串
    */
   public static String unicodeToUtf8(String theString) {
       char aChar;
       int len = theString.length();
       StringBuffer outBuffer = new StringBuffer(len);
       for (int x = 0; x < len;) {
           aChar = theString.charAt(x++);
           if (aChar == '\\') {
               aChar = theString.charAt(x++);
               if (aChar == 'u') {
                   // Read the xxxx
                   int value = 0;
                   for (int i = 0; i < 4; i++) {
                       aChar = theString.charAt(x++);
                       switch (aChar) {
                       case '0':
                       case '1':
                       case '2':
                       case '3':
                       case '4':
                       case '5':
                       case '6':
                       case '7':
                       case '8':
                       case '9':
                           value = (value << 4) + aChar - '0';
                           break;
                       case 'a':
                       case 'b':
                       case 'c':
                       case 'd':
                       case 'e':
                       case 'f':
                           value = (value << 4) + 10 + aChar - 'a';
                           break;
                       case 'A':
                       case 'B':
                       case 'C':
                       case 'D':
                       case 'E':
                       case 'F':
                           value = (value << 4) + 10 + aChar - 'A';
                           break;
                       default:
                           throw new IllegalArgumentException(
                                   "Malformed   \\uxxxx   encoding.");
                       }
                   }
                   outBuffer.append((char) value);
               } else {
                   if (aChar == 't')
                       aChar = '\t';
                   else if (aChar == 'r')
                       aChar = '\r';
                   else if (aChar == 'n')
                       aChar = '\n';
                   else if (aChar == 'f')
                       aChar = '\f';
                   outBuffer.append(aChar);
               }
           } else
               outBuffer.append(aChar);
       }
       return outBuffer.toString();
   }

   /**
    * UTF8转Unicode
    * @param theString 原字符串
    * @return String 转码后字符串
    */
   public static String utf8ToUnicode(String theString) {
       char[] myBuffer = theString.toCharArray();

       StringBuffer sb = new StringBuffer();
       for (int i = 0; i < theString.length(); i++) {
           UnicodeBlock ub = UnicodeBlock.of(myBuffer[i]);
           if (ub == UnicodeBlock.BASIC_LATIN) {
               sb.append(myBuffer[i]);
           } else if (ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
               int j = (int) myBuffer[i] - 65248;
               sb.append((char) j);
           } else {
               short s = (short) myBuffer[i];
               String hexS = Integer.toHexString(s);
               String unicode = "\\u" + hexS;
               sb.append(unicode.toLowerCase());
           }
       }
       return sb.toString();
   }
   
}
